<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@ page import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Edit Student Enrollment Detail</title>
	
	<link rel="stylesheet" type="text/css" href="css/stylev2.css" />
	<link rel="stylesheet" type="text/css" href="css/student-system.css" />
	<link rel="stylesheet" type="text/css" href="css/frame.css" />
	<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
		<script type="text/javascript" src="js/bootbox.js"></script>
	
	<style type="text/css">
		td {
			height: 50px;
		}
		
		input {
			font-size: 20px;
		}
		
		select {
			font-size: 20px;
		}
		a {
	      text-decoration:none;
	   	}
	</style>
	
<script type="text/javascript">

</script>	
	
</head>
<body>

<%
//get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
	

	// check session
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin_home.jsp"> 	
	<%}
	
	//get permission 
	
	String name = String.valueOf(session.getAttribute("name"));
	String surname = String.valueOf(session.getAttribute("surname"));
	String role_name = String.valueOf(session.getAttribute("enrollment_role"));
	
	String department_id = String.valueOf(session.getAttribute("department_id"));

	
			
	System.out.println("name : " + name);///////////////////////////////
	System.out.println("department_id : " + department_id);/////////////////////////
	
	
	List<String> system_name = new ArrayList<String>();
	
	Permission permission = new Permission(strUser,name);
	permission.setDepartmentId(department_id);
	permission.setRegisCreate(Boolean.valueOf(String.valueOf(session.getAttribute("enrollment_create"))));
	permission.setRegisDelete(Boolean.valueOf(String.valueOf(session.getAttribute("enrollment_delete"))));
	permission.setRegisRead(Boolean.valueOf(String.valueOf(session.getAttribute("enrollment_read"))));
	permission.setRegisUpdate(Boolean.valueOf(String.valueOf(session.getAttribute("enrollment_update"))));
	
	String permiss = "";
	if(permission.getRegisCreate()) {
		permiss = permiss + "Add";
	}
	if(permission.getRegisUpdate()) {
		if("".equals(permiss)) {
			permiss = permiss + "Edit";
		} else {
			permiss = permiss + " Edit";
		}
	}
	if(permission.getRegisRead()) {
		if("".equals(permiss)) {
			permiss = permiss + "Read";
		} else {
			permiss = permiss + " Read";
		}
	}
	if(permission.getRegisDelete()) {
		if("".equals(permiss)) {
			permiss = permiss + "Delete";
		} else {
			permiss = permiss + " Delete";
		}
	}
	
	//check permission
	if(!permission.getRegisUpdate()){%>
		<script>alert("This user dont have PERMISSION to access this zone.");</script>
		<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=enroll_home.jsp"> 	
	<%}
	
	//set Database Connection
	String hostProps = "";
	String usernameProps  = "";
	String passwordProps  = "";
	
	try {
		//get current path
		ServletContext servletContext = request.getSession().getServletContext();
		
		InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
		Properties props = new Properties();
		
		props.load(input);

		hostProps  = props.getProperty("host");
		usernameProps  = props.getProperty("username");
		passwordProps  = props.getProperty("password");
	} catch (Exception e) { 
		out.println(e);  
	}

	System.out.println("hostProperties : "+hostProps);////////////////try/////////////////
	System.out.println("usernameProperties : "+usernameProps);////////////////try/////////////////
	System.out.println("passwordProperties : "+passwordProps);////////////////try/////////////////
	
	// connect database
	Connection connect = null;		
	try {
		Class.forName("com.mysql.jdbc.Driver");
	
		connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/AIMS" +
				"?user=" + usernameProps  + "&password=" + passwordProps );
	
		if(connect != null){
			System.out.println("Database Connect Sucesses.");
		} else {
			System.out.println("Database Connect Failed.");	
		}

	} catch (Exception e) {
		out.println(e.getMessage());
		e.printStackTrace();
	}
		
	//GET Info
	String student_id = request.getParameter("student_id");
	String class_id = request.getParameter("class_id");
	String type = request.getParameter("type");
	String grade = request.getParameter("grade");
	String course_id = request.getParameter("course_id");
	String course_name = request.getParameter("course_name");
	
	String checker = request.getParameter("checker");
 	String counter = request.getParameter("counter");
 	
 	System.out.println("checker OLD : "+checker);////////////////////try///////////////////
 	System.out.println("counter OLD : "+counter);////////////////////try///////////////////
 	
/////////////////////////////////////////////////////////////////////////////////////////////////

		if(checker.equals("edit")) {
				System.out.println("SAVE JAAAAAA");
				request.setCharacterEncoding("UTF-8");
				
				String tempgrade = request.getParameter("grade").toUpperCase();
				System.out.println("tempgrade OLD : "+tempgrade);////////////////////try///////////////////
				String temptype =request.getParameter("type").toUpperCase();
				System.out.println("temptype OLD : "+temptype);////////////////////try///////////////////

				boolean tempCheckgrade = tempgrade.matches("[-+]?\\d+(\\.\\d+)?");
				boolean tempChecktype = temptype.matches("[-+]?\\d+(\\.\\d+)?");
				
				boolean checker_letter = false;
				boolean checker_type = false;
				
			 	if(tempgrade.equals("") || temptype.equals("")){
			 		if(tempgrade.equals("") ){%>
					<script>alert("กรุณาใส่เกรด");</script>
					<%}
						else if(temptype.equals("")){%>
						<script>alert("กรุณาใส่สถานะของเกรด");</script>
					<%}
			 	}
			 	
			 	if(!tempgrade.equals("X") && !tempgrade.equals("A") && !tempgrade.equals("B+") && !tempgrade.equals("B") && !tempgrade.equals("C+") && !tempgrade.equals("C") && !tempgrade.equals("D+") && !tempgrade.equals("D") && !tempgrade.equals("F") && !tempgrade.equals("P") && !tempgrade.equals("S") && !tempgrade.equals("T") && !tempgrade.equals("U") && !tempgrade.equals("W"))
			 	{
			 		checker_letter = true;
			 	}
			 	
			 	if(!temptype.equals("N") && !temptype.equals("AU") && !temptype.equals("A"))
			 	{
			 		checker_type = true;
			 	}
			 	
				 	if(checker_letter){//checker	
				 			counter = "none" ;%>
							<script language="javascript"> alert("กรุณาใส่เกรดด้วย \nX,A,B+,B,C+,C,D+,D,F,P,S,T,U,W ค่ะ");</script>
							
							
					<%}else if(checker_type){
							
							%><script language="javascript"> alert("กรุณาใส่สถานะเกรดด้วย \nAU สำหรับ Audit, N สำหรับ Normal หรือ A สำหรับ Add ค่ะ");</script><%
							counter = "none" ;
					}
					else if(!tempCheckgrade && !tempChecktype){
			 		
					// get value from tag
					String gradeNew = request.getParameter("grade").toUpperCase();
					String typeNew =request.getParameter("type").toUpperCase();
					
					System.out.println("tempgrade OLD : "+tempgrade);////////////////////try///////////////////
					System.out.println("gradeNew NEW : "+gradeNew);////////////////////try///////////////////
					System.out.println("temptype OLD : "+temptype);////////////////////try///////////////////
					System.out.println("typeNew NEW : "+typeNew);////////////////////try///////////////////
					
					String test = "UPDATE `enrollment` SET `grade`='"+ gradeNew + "', "
							+ " `type_of_enrollment`='" + typeNew +"'"							
							+ " WHERE student_id='"+student_id+"' AND class_id="+class_id+")";
							
					System.out.println("test : "+test);////////////////////try///////////////////		
					
					try {							
						connect.createStatement().executeUpdate("UPDATE `enrollment` SET `grade`='"+ gradeNew + "', "
								+ " `type_of_enrollment`='" + typeNew +"'"							
								+ " WHERE student_id='"+student_id+"' AND class_id="+class_id);
						
						counter = "counter";
						
	%>					
						<script language="javascript"> alert("แก้ไขข้อมูลสำเร็จ");	</script>
						
						
	<%
					} catch (SQLException e) {
						e.printStackTrace();
					}				
				}
		}
 	
		connect.close();
	%>	


	

	<div id="page-wrap">
			<div id="inside">
				<table border="0">
					<tr style="font-size:24px">
						<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
							<div id="header_banner-2"></div>
						</td>
						
						<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
							<p align="right"><label><font size="4" color="#ffffff">username : <%=strUser%>&nbsp;&nbsp;</font></label></p>
							<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
						</td>
					</tr>
				</table>
        		
        		<div id="main-content-ac">
        					<div class="imageFrame">แก้ไขวิชาลงทะเบียน</div>
							<br/><br/><br/><br/><br/>
        						<center>        						      						
	        						<table border="0" width="800"> 									
										<tr style="font-size:20px">
											<form id="form" name="form" method="post" action="enroll_student_enrollment_detail_edit.jsp">  	
											
													<input type="hidden" id="checker" name="checker" value="edit" />
													 <input type="hidden" id="student_id" name="student_id" value="<%=student_id%>" />
												     <input type="hidden" id="class_id" name="class_id" value="<%=class_id%>" />												     												     
												     <input type="hidden" id="course_id" name="course_id" value="<%=course_id%>" />
			                 						 <input type="hidden" id="course_name" name="course_name" value="<%=course_name%>" />
			                 						  
			                 						 
												<tr>
													<td>
														<label for="labelcourse"> <font size="5"><strong>วิชา: <%=course_id %>-<%=course_name %></strong></font></label>
													</td>
												</tr>	
												<tr>
													<td>
														<label for="labelCredit"> <font size="4"><strong>เกรด [ ex. A, B+, B ]</strong></font> </label>
														<label><font size="5" color="#FF0000">*</font></label></td>
													<td>
														<input type="text" name="grade" id="grade" maxlength="2" required value="<%=grade%>" />
														<input type="hidden" id="grade" name="grade" value="<%=grade%>" />	
													</td>
												</tr>
												<tr>	
													<td>
														<label for="labelCredit"> <font size="4"><strong>สถานะของเกรด  [ ex. N (normal), AU (audit), A(adds) ]</strong></font> </label>
														<label><font size="5" color="#FF0000">*</font></label>
													</td>
													<td>
														<input type="text" name="type" id="type" maxlength="2" required value="<%=type%>" />  
														<input type="hidden" id="type" name="type" value="<%=type%>" />
													</td>
													
												</tr>
												
										<tr>
									
												<td></td>
										<%
											if(counter.equals("none")){
										%>
												<input type="hidden" id="counter" name="counter" value="counter" />
												<td><input type="submit" id="btn_edit_info" value="edit" name="edit" class="buttonStyle" /></td>
										<%
										
											}
										%>
										
										</form>	
										<form id="form2" name="form2" method="post" action="enroll_student_enrollment_detail.jsp"> 
										<%if(counter.equals("counter")){%>
											
										
											<input type="hidden" id="student_id" name="student_id" value="<%=student_id%>" />
											<input type="hidden" id="counter" name="counter" value="none" />
											<td><input type="submit" id="back" value="back" name="back" class="buttonStyle" /></td>
											
										<%
											}									
										%>
										</form>										
										
																
											
										</tr>
								</table>
							</center>
							
						<br><br><br><br><br><br><br><br>	
						</div>
					<div style="clear: both;"></div>
						<div id="footer"></div>
			</div>		
						<div style="clear: both;"></div>		
	</div>
		
</body>
</html>